package com.zhouxiaoge.lambda;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/****************************************************************
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 卐卐卐                    _ooOoo_                    卍卍卍卍卍卍
 卐卐卐      卐           o8888888o          卍       * 佛  卍卍卍
 卐卐卐                  88'' . ''88                  * 祖  卍卍卍
 卐卐卐                  ( | -_- | )                  * 保  卍卍卍
 卐卐卐                   0\  =  /0                   * 佑  卍卍卍
 卐卐卐                ____/'---'\____                *     卍卍卍
 卐卐卐              .'  \\|     |//  '.              * 永  卍卍卍
 卐卐卐             /  \\|||  :  |||//  \             * 无  卍卍卍
 卐卐卐            /  _||||| -:- |||||-  \            * BUG 卍卍卍
 卐卐卐            |   | \\\  -  /// |   |            *     卍卍卍
 卐卐卐            | \_|  ''\---/''  |   |            * 阿  卍卍卍
 卐卐卐            \  .-\__  `-`  ___/-. /            * 弥  卍卍卍
 卐卐卐          ___`. .'  /--.--\  `. . __           * 陀  卍卍卍
 卐卐卐        ."" '<  ‘.___\_<|>_/___.’  >'"".     * 佛  卍卍卍
 卐卐卐       | | :  ` - `.;`\ _ /`;.`/ - ` : | |     卍卍卍卍卍卍
 卐卐卐       | \ `-.   \_ __\ /__ _/   .-`  / /      卍卍卍卍卍卍
 卐卐卐 ======`-.____`-.___\_____/___.-`____.-'=======卍卍卍卍卍卍
 卐@author 周明阳 mail:gqzmy@outlook.com 时间:2019-06-21 15:30卍卍
 卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍卐卍
 *
 *
 */

public class Lamdba3Test {
    // Consumer<T> 消费型接口
    @Test
    public void consumerTest() {
        happy(10000L, m -> System.out.println("大保健消费金额：" + m + "元"));
    }

    private void happy(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

    // Supplier<T> 供给型接口
    @Test
    public void supplierTest() {
        List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));
        System.out.println(numList);
    }

    private List<Integer> getNumList(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }

    // Function<T, R> 函数型接口
    @Test
    public void functionTest() {
        String handler = strHandler("\t\t\t 我是谁,Hello World !   ", str -> str.trim().toUpperCase());
        System.out.println(handler);
    }

    private String strHandler(String string, Function<String, String> function) {
        return function.apply(string);
    }

    // Predicate<T> 断言型接口
    @Test
    public void predicateTest() {
        List<String> list = Arrays.asList("Hello", "World", "Java", "Python");
        List<String> filterStr = filterStr(list, s -> s.length() > 4);
        System.out.println(filterStr);

    }

    private List<String> filterStr(List<String> strings, Predicate<String> predicate) {
        List<String> list = new ArrayList<>();
        for (String s : strings) {
            if (predicate.test(s)) {
                list.add(s);
            }
        }
        return list;
    }
}
